<template>
    <div class="info-collect">
        <Crumb name="Unit Option Field"></Crumb>

        <div class="content">
            <el-button
                    type="primary"
                    @click="openAdd"
            >Add New Unit Type
            </el-button>
            <Search>
                <el-select v-model="fieldId">
                    <el-option v-for="item in fieldList" :key="item.id" :value="item.id" :label="item.fieldName"/>
                </el-select>
            </Search>
            <div class="table-wrapper">
                <el-table
                        :data="typeList"
                        empty-text="No Data"
                        border
                        stripe
                        style="width: 100%">

                    <el-table-column
                            prop="typeName"
                            label="Type"
                            width="180"
                    >
                    </el-table-column>
                    <el-table-column label="Opr" >
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    @click="openEdit(scope.row)">Modify
                            </el-button>
                            <!--<el-button-->
                                    <!--size="mini"-->
                                    <!--type="danger"-->
                                    <!--@click="console.log(scope.$index, scope.row)">Abandon-->
                            <!--</el-button>-->
                        </template>
                    </el-table-column>
                </el-table>
                <!--<div class="page">-->
                    <!--<el-pagination background-->
                                   <!--:page-size=10-->
                                   <!--@current-change="changePage"-->
                                   <!--layout="prev, pager, next"-->
                                   <!--:total="total"> </el-pagination>-->
                <!--</div>-->

            </div>

            <div class="modal-wrapper">
                <el-dialog
                        title="Unit Type"
                        top="10"
                        :visible.sync="dialogVisible"
                        width="80%"
                        >
                    <el-form :model="ruleForm"  ref="ruleForm" label-width="200px" class="demo-ruleForm">
                        <el-form-item label="Type">
                            <el-input v-model="ruleForm.typeName"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false">Cancel</el-button>
                        <el-button type="primary" @click="submitForm">Confirm</el-button>
                    </span>
                </el-dialog>
            </div>
        </div>
    </div>
</template>

<script>
    import Crumb from '@/components/common/crumb'
    import Search from '@/components/common/search'
    import {getUnitFieldList,getTypeList,modType,addType} from "../../api/unit";

    export default {
        name: "info-collect",
        data() {
            return {
                ruleForm: {},
                fieldList:[],
                typeList:[],
                fieldId:'',
                dialogVisible:false
            }
        },
        created(){
            this._getFieldList();
        },
        methods: {
           async submitForm(){
                      if(!this.fieldId){
                          return;
                      }
                        if (this.ruleForm.id) {
                            let {status} = await modType(this.ruleForm);
                            if(status === 0 ){
                                const target = this.typeList.find(item=>item.id === this.ruleForm.id);
                                this.$set(target,this.ruleForm);
                                this.$message({
                                    message:'Modify Successfully',
                                    type:'success'
                                });
                                this.clear();
                                this.dialogVisible = false
                            }
                        } else {
                            let {status} = await addType({fieldId:this.fieldId,typeName:this.ruleForm.typeName});
                            if (status  === 0 ) {
                                this.$message({
                                    message:'Add Successfully',
                                    type:'success'
                                });
                                this._getTypeList(this.fieldId);
                                this.clear();
                                this.dialogVisible = false
                            }
                        }


            },
            clear(){
                this.ruleForm= {};
            },
            openAdd(){
                this.clear();
                this.dialogVisible = true
            },
            openEdit(r){
                this.ruleForm = r;
                this.dialogVisible = true

            },
            async _getFieldList(){
                let {status,data,msg} = await getUnitFieldList();
                if(status === 0 ) {
                    this.fieldList = data;
                    this.total = data.length
                } else {
                    this.$message({
                        type:'error',
                        message:msg
                    })
                }
            },
            async _getTypeList(fieldId){
                if(fieldId) {
                    const {status,data} = await getTypeList({fieldId});
                    if(status===0) {
                        this.typeList = data
                    } else {
                        this.typeList = []
                    }
                }
            },
        },
        components: {
            Crumb, Search
        },
        watch:{
            fieldId(id){
                this._getTypeList(id)
            }
        }
    }
</script>

<style scoped lang="scss">
    .info-collect {
        background: #f6;
        .content {
            text-align: left;
            padding: 20px 0;
            .table-wrapper {
                margin-top: 20px;
            }
            .page {
                text-align: center;
                margin-top: 10px;
            }
        }
    }
</style>